﻿<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
 
    <script type='text/html' template lay-done="layui.data.formdone(d.params)"> 
        <input type='hidden' name='PID' value="{{ d.params.PID || '' }}"/>
    </script>   
    <div class='layui-form-item'>
        <label class='layui-form-label'>自提点类型<span style="color:red">*</span></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <select id="Type" name="Type" lay-verify='required' lay-filter="seltype">
                    <option value='0' {{ d.params.Type == '0' ? 'selected' : '' }}>供应商自提点</option>
                    <option value='1' {{ d.params.Type == '1' ? 'selected' : '' }}>网点自提点</option>
                </select>
            </script>
        </div>
    </div>   
    <div class='layui-form-item'>
        <label class='layui-form-label'><span id="sourcespan">供应商/网点ID</span><span style="color:red">*</span></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <!--<select id="SourceID" name="SourceID" lay-verify='required' lay-search lay-filter="selsource">

                </select>-->
                <select name="select1" id="select1" xm-select="select1" xm-select-radio="" lay-verify='required' xm-select-search xm-select-skin="default">
                    <option value=""></option>
                </select>
                <input type="hidden" name="SourceID" id="SourceID" value='{{ d.params.SourceID || '' }}'>
            </script>
        </div>
    </div>   
    <div class='layui-form-item'>
        <label class='layui-form-label'>自提点地址<span style="color:red">*</span></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='Name' value='{{ d.params.Name || '' }}' lay-verify='required' placeholder='自提点' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>   
    <div class='layui-form-item'>
        <label class='layui-form-label'>联系人<span style="color:red">*</span></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='UserName' value='{{ d.params.UserName || '' }}' lay-verify='required' placeholder='联系人' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>   
    <div class='layui-form-item'>
        <label class='layui-form-label'>联系电话<span style="color:red">*</span></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='Phone' value='{{ d.params.Phone || '' }}' lay-verify='required' placeholder='联系电话' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>   
    <!--<div class='layui-form-item'>
        <label class='layui-form-label'>详细地址</label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='Address' value='{{ d.params.Address || '' }}' lay-verify='required' placeholder='详细地址' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>-->   
    <div class='layui-form-item'>
        <label class='layui-form-label'>坐标<span style="color:red">*</span></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <button type="button" class="layui-btn layui-btn-normal" onclick="SelGps()">选择坐标</button>
                <span style="margin-left:12px;font-weight:bold" id="spangps">{{ d.params.Gps || '' }}</span>
                <input type='hidden' name='Gps' id="Gps" value='{{ d.params.Gps || '' }}'>
            </script>
        </div>
    </div>    
    <div class='layui-form-item'>
        <label class='layui-form-label'>自提时间段<span style="color:red">*</span></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='TimeSlot' value='{{ d.params.TimeSlot || '' }}' lay-verify='required' placeholder='自提时间段' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>   
    <div class='layui-form-item'>
        <label class='layui-form-label'>状态</label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <select id="State" name="State" lay-verify='required'>
                    <option value='0' {{ d.params.State == '0' ? 'selected' : '' }}>启用</option>
                    <option value='1' {{ d.params.State == '1' ? 'selected' : '' }}>停用</option>
                </select>
            </script>
        </div>
    </div>   
    <div class='layui-form-item'>
        <label class='layui-form-label'>备注</label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='RMK' value='{{ d.params.RMK || '' }}' lay-verify='' placeholder='备注' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div> 
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" value="确认" class="layui-btn">
        </div>
    </div>
</div>

<script type="text/javascript"> 

    var user = null;
    var defaultgps = "";

    var from, admin, $;
    layui.use(['form','admin','upload','laydate'], function () {
        form = layui.form;//required（必填项）phone（手机号）email（邮箱）url（网址）number（数字）date（日期）identity（身份证） 自定义值
        admin = layui.admin;
        $ = layui.$;

        if (user == null) {
            GetUser();
        }

        form.on("select(seltype)", function (d) {
            $("#SourceID").val("");
            defaultgps = "";
            $("#spangps").text(defaultgps);
            $("#Gps").val(defaultgps);
            form.render('select');
            if (d.value == 0) {
                $("#sourcespan").text("供应商");
                LoadSuppliers("");
            } else {
                $("#sourcespan").text("店铺");
                LoadStores("");
            }
        });

        form.on("select(selsource)", function (d) {
            var Type = $("#Type").val();
            if (Type == 1) {
                admin.req({
                    url: layui.setter.APIUrl + '/shop/SelfpointMgr/GetGps?storeid='+ d.value, //实际使用请改成服务端真实接口
                    type: 'post',
                    done: function (res) {
                        defaultgps = res.ResultInfo;
                        $("#spangps").text(defaultgps);
                        $("#Gps").val(defaultgps);
                    }
                });
            }
        })

    });

    layui.data.formdone = function (d) {
        admin = layui.admin;
        form = layui.form;
        $ = layui.$;

        if (user == null) {
            GetUser();
        }

        if ((d.PID != "" && d.PID != null) || user.UserType_Sel == 1 || user.UserType_Sel == 2) {
            $('#Type').attr("disabled", true);
        }
        if (d.PID == "" || d.PID == null) {
            if (user.UserType_Sel == 2) {
                $("#sourcespan").text("供应商");
                $("#Type").val(0);
                form.render('select');
                LoadSuppliers(d.SourceID);
            } else {
                $("#sourcespan").text("店铺");
                $("#Type").val(1);
                form.render('select');
                LoadStores(d.SourceID);
            }
        } else {
            defaultgps = d.Gps;
            $("#spangps").text(defaultgps);
            $("#Gps").val(defaultgps);

            if (d.Type != 1) {
                $("#sourcespan").text("供应商");
                LoadSuppliers(d.SourceID);
            } else {
                $("#sourcespan").text("店铺");
                LoadStores(d.SourceID);
            }
        }
    }
    
    function LoadSuppliers(SourceID) {

        //admin.req({
        //    url: layui.setter.APIUrl + '/shop/SupplierMgr/customerQuery', //实际使用请改成服务端真实接口
        //    type: 'post',
        //    done: function (res) {
        //        var html = "<option value=''></option>";
        //        layui.each(res.Data, function (index, eachdata) {
        //            var strIsSel = '';
        //            if (eachdata.PID == SourceID)
        //                strIsSel = "selected='selected'";
        //            html += "<option value='" + eachdata.PID + "' " + strIsSel + ">" + eachdata.Name + "</option>";
        //        });
        //        $("#SourceID").html(html);

        //        if ((SourceID != "" && SourceID != null) || user.UserType_Sel == 1 || user.UserType_Sel == 2) {
        //            $('#SourceID').attr("disabled", true);
        //        }
        //        form.render('select');
        //    }
        //});

        layui.use(['admin', 'form', 'formSelects'], function () {
            $ = layui.$;
            form = layui.form;
            admin = layui.admin;
            var formSelects = layui.formSelects;

            if (SourceID == "" || SourceID == null) {
                if (user.UserType_Sel == 1)
                    SourceID = user.StoreID;
                if (user.UserType_Sel == 2) {
                    SourceID = user.SupplierID;
                }
            }

            formSelects.config('select1', {
                keyName: 'title',            //自定义返回数据中name的key, 默认 name
                keyVal: 'id',            //自定义返回数据中value的key, 默认 value
                keyChildren: 'children',    //联动多选自定义children
            });

            admin.req({
                url: layui.setter.APIUrl + '/shop/supplierMgr/customerQueryChildren2', //实际使用请改成服务端真实接口
                type: 'post',
                done: function (res) {
                    layui.formSelects.data('select1', 'local', {
                        arr: res.Data,
                        linkage: true,
                        linkageWidth: 120
                    });
                    var selID = SourceID;
                    if (selID != "" && selID != undefined) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/supplierMgr/GetModel?key=' + selID, //实际使用请改成服务端真实接口
                            type: 'post',
                            done: function (res) {
                                //如果有需要默认值的小伙伴请使用formSelects.
                                formSelects.value('select1', [res.Data.Area + "/" + res.Data.Level_Sel + res.Data.Area + "/" + selID]);
                            }
                        });
                    }
                    if ((SourceID != "" && SourceID != null) || user.UserType_Sel == 1 || user.UserType_Sel == 2) {
                        formSelects.disabled('select1');
                    }
                }
            });
            formSelects.on('select1', function (id, vals, val, isAdd, isDisabled) {
                if (isAdd == true) {
                    if (val.value.indexOf("/") > 0 && ((val.value.split('/')).length - 1) >= 2) {  //多级选择时,第一级无法选中  供应商前两级都无法选中
                        var value = val.value.split('/')[val.value.split('/').length - 1];
                        $("#SourceID").val(value);  //实际选中效果为多个ID组合 ‘id1/id2/id3’,取最后一个
                    } else {
                        isAdd = false;
                        return false;
                    }
                } else {
                    $("#SourceID").val("");
                }
            });

        });

    }

    function LoadStores(SourceID) {

        //admin.req({
        //    url: layui.setter.APIUrl + '/shop/StoreMgr/customerQuery', //实际使用请改成服务端真实接口
        //    type: 'post',
        //    done: function (res) {
        //        var html = "<option value=''></option>";
        //        layui.each(res.Data, function (index, eachdata) {
        //            var strIsSel = '';
        //            if (eachdata.PID == SourceID) {
        //                strIsSel = "selected='selected'";
        //                if (defaultgps == "") {
        //                    defaultgps = eachdata.Gps;
        //                    $("#spangps").text(defaultgps);
        //                    $("#Gps").val(defaultgps);
        //                }
        //            }
        //            html += "<option value='" + eachdata.PID + "' " + strIsSel + ">" + eachdata.Name + "</option>";
        //        });
        //        $("#SourceID").html(html);

        //        if ((SourceID != "" && SourceID != null) || user.UserType_Sel == 1 || user.UserType_Sel == 2) {
        //            $('#SourceID').attr("disabled", true);
        //        }
        //        form.render('select');
        //    }
        //});


        layui.use(['admin', 'form', 'formSelects'], function () {
            $ = layui.$;
            form = layui.form;
            admin = layui.admin;
            var formSelects = layui.formSelects;

            if (SourceID == "" || SourceID == null) {
                if (user.UserType_Sel == 1)
                    SourceID = user.StoreID;
                if (user.UserType_Sel == 2) {
                    SourceID = user.SupplierID;
                }
            }

            formSelects.config('select1', {
                keyName: 'title',            //自定义返回数据中name的key, 默认 name
                keyVal: 'id',            //自定义返回数据中value的key, 默认 value
                keyChildren: 'children',    //联动多选自定义children
            });

            admin.req({
                url: layui.setter.APIUrl + '/shop/storeMgr/customerQueryChildren2', //实际使用请改成服务端真实接口
                type: 'post',
                done: function (res) {
                    layui.formSelects.data('select1', 'local', {
                        arr: res.Data,
                        linkage: true,
                        linkageWidth: 120
                    });
                    var selID = SourceID;
                    if (selID != "" && selID != undefined) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/storeMgr/GetModel?key=' + selID, //实际使用请改成服务端真实接口
                            type: 'post',
                            done: function (res) {
                                //如果有需要默认值的小伙伴请使用formSelects.value
                                formSelects.value('select1', [res.Data.Area + "/" + selID]);
                            }
                        });
                        if (defaultgps == "") {
                            LoadGps(selID);
                        }
                    }
                    if ((SourceID != "" && SourceID != null) || user.UserType_Sel == 1 || user.UserType_Sel == 2) {
                        debugger;
                        formSelects.disabled('select1');
                    }
                }
            });
            formSelects.on('select1', function (id, vals, val, isAdd, isDisabled) {
                if (isAdd == true) {
                    if (val.value.indexOf("/") > 0) {  //多级选择时,第一级无法选中
                        var value = val.value.split('/')[val.value.split('/').length - 1];
                        $("#SourceID").val(value);  //实际选中效果为多个ID组合 ‘id1/id2/id3’,取最后一个
                        LoadGps(value);
                    } else {
                        isAdd = false;
                        return false;
                    }
                } else {
                    $("#SourceID").val("");
                }
            });

        });

    }

    function GetUser() {
        admin = layui.admin;
        $ = layui.$;

        admin.req({
            url: layui.setter.APIUrl + '/admin/userMgr/GetUserModel', //实际使用请改成服务端真实接口
            type: 'post',
            async: false,
            done: function (res) {
                user = res.Data;
            }
        });
    }


    layui.data.getSelSource = function (d,sourceName) { 
         layui.use(['admin','form'], function () {
                var $ = layui.$;
                var form = layui.form;
                var admin = layui.admin;
     
        });
    }
 
     
    layui.data.setUpload = function (d) { 
        layui.use(['upload','admin'], function () {
                var $ = layui.$,
                upload = layui.upload,
                admin = layui.admin;
                var formSelects = layui.formSelects;
      
        });
    }

    
    layui.data.datedone = function (d) { 
        layui.use(['laydate'], function () {
                var laydate = layui.laydate;
     
        });
    }

    function LoadGps(value) {
        admin.req({
            url: layui.setter.APIUrl + '/shop/SelfpointMgr/GetGps?storeid=' + value, //实际使用请改成服务端真实接口
            type: 'post',
            done: function (res) {
                defaultgps = res.ResultInfo;
                $("#spangps").text(defaultgps);
                $("#Gps").val(defaultgps);
            }
        });
    }

    //弹出一个iframe层
    function SelGps() {
        layer.open({
            type: 2,
            title: '选择地址',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['70%', '80%'],
            content: '/map.html?v=1&number=0&center=' + defaultgps
        });
    };

    function SelGpsLoad(gps) {
        //alert(gps);
        $("#spangps").text(gps);
        $("#Gps").val(gps);
        defaultgps = gps;
    }

</script>
